package com.p2p.service.fundManagementService.impl.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.p2p.common.DTO.FundsDTO;
import com.p2p.common.base.RespResult;
import com.p2p.common.base.RespResultCode;
import com.p2p.config.RedisConfig;
import com.p2p.mapper.RechargeRecordMapper;

import com.p2p.service.fundManagementService.impl.RechargeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 充值管理信息
 */
@Service
@Slf4j
public class RechargeRecordServiceImpl implements RechargeRecordService {

    @Autowired
    RechargeRecordMapper rechargeRecordMapper;

    @Autowired
    RedisConfig redisConfig;

    /**
     * 查找所有充值记录
     * @param currentPage 当前页
     * @param pageSize 每页大小
     * @return RechargeRecordList
     */
    @Override
    public RespResult getAllRechargeRecordList(Integer currentPage, Integer pageSize) {

        Page<FundsDTO> page;

        RedisTemplate<String, Object> redisTemplate = redisConfig.redisTemplate();

        redisTemplate.setHashKeySerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());

        HashOperations<String, Object, Object> stringObjectObjectHashOperations = redisTemplate.opsForHash();

        page = (Page<FundsDTO>) stringObjectObjectHashOperations.get("funds", "rechargeRecord");

        if (page != null) {
            log.info("直接读取redis数据"+page);
            return RespResult.success(page);
        }
        else if (currentPage != null && pageSize != null) {

                page = new Page(currentPage,pageSize);
                List<FundsDTO> fundsDTOS = rechargeRecordMapper.selectAllRechargeRecordList(page);
                page.setRecords(fundsDTOS);
                stringObjectObjectHashOperations.put("funds","rechargeRecord", page);
                return RespResult.success(page);
        }
        return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(),RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(),null);
    }

    /**
     * 根据充值记录查询充值列表
     * @param currentPage 当前页
     * @param pageSize 每页大小
     * @param rechargeOddNumber 充值单号
     * @return RechargeRecordList
     */
    @Override
    public RespResult getAllRechargeRecordListByRechargeOddNumber(Integer currentPage, Integer pageSize,String rechargeOddNumber) {

        if (currentPage != null && pageSize != null && rechargeOddNumber != null) {
            Page<FundsDTO>  page = new Page(currentPage,pageSize);
            List fundsDTOS = rechargeRecordMapper.selectAllRechargeRecordListByRechargeOddNumber(page, rechargeOddNumber);
            page.setRecords(fundsDTOS);

            return RespResult.success(page);
        }
        return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(),RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(),null);

    }

    /**
     * 根据电话查找充值信息
     * @param currentPage 当前页
     * @param pageSize 每页大小
     * @param phone 电话号码
     * @return 充值列表
     */
    @Override
    public RespResult getAllRechargeRecordListByPhone(Integer currentPage, Integer pageSize,String phone) {

        if (currentPage != null && pageSize != null && phone != null) {
            Page<FundsDTO> objectPage = new Page<>();
            List<FundsDTO> fundsDTOS = rechargeRecordMapper.selectAllRechargeRecordListByPhone(objectPage, phone);

            objectPage.setRecords(fundsDTOS);

            return RespResult.success(objectPage);
        }
        return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(),RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(),null);
    }

    /**
     * 根据支付方式查找充值列表
     * @param currentPage 当前页
     * @param pageSize 每页大小
     * @param rechargeMethod 充值方式
     * @return 充值列表
     */
    @Override
    public RespResult getAllRechargeRecordListByRechargeMethod(Integer currentPage, Integer pageSize,String rechargeMethod) {

        if (currentPage != null && pageSize != null && rechargeMethod != null) {
            Page<FundsDTO> page = new Page();
            List<FundsDTO> fundsDTOS = rechargeRecordMapper.selectAllRechargeRecordListByRechargeMethod(page, rechargeMethod);
            page.setRecords(fundsDTOS);

            return RespResult.success(page);
        }
        return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(),RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(),null);
    }

    /**
     * 根据充值时间查找列表
     * @param currentPage 当前页
     * @param pageSize 每页大小
     * @param startTime 开始时间
     * @param stopTime 结束时间
     * @return 充值列表
     */
    @Override
    public RespResult getAllRechargeRecordListByTime(Integer currentPage, Integer pageSize,String startTime,String stopTime) {

        if (currentPage != null && pageSize != null && startTime != null && stopTime != null) {
            Page<FundsDTO> objectPage = new Page<>(currentPage,pageSize);
            List<FundsDTO> fundsDTOS = rechargeRecordMapper.selectAllRechargeRecordListByTime(objectPage, startTime, stopTime);

            objectPage.setRecords(fundsDTOS);

            return RespResult.success(objectPage);
        }
        return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(),RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(),null);
    }

    /**
     * 根据状态查找充值列表
     * @param currentPage
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public RespResult getAllRechargeRecordListByStatus(Integer currentPage, Integer pageSize,Byte status) {

        if (currentPage != null && pageSize != null && status != null) {
            Page<FundsDTO> objectPage = new Page<>(currentPage,pageSize);
            List<FundsDTO> fundsDTOS = rechargeRecordMapper.selectAllRechargeRecordListByStatus(objectPage,status);

            objectPage.setRecords(fundsDTOS);

            return RespResult.success(objectPage);
        }
        return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(),RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(),null);
    }

    @Override
    public RespResult exportRechargeRecordList(Integer currentPage, Integer pageSize) {
        return null;
    }
}
